import axios from 'axios'
import qs from 'qs'

const httpService = axios.create({
    baseURL:'http://220.166.57.71:8087/',
    // timeout:30000
})

httpService.interceptors.request.use(
    config => {
        if (true) {
            // config.headers['User-Token'] = ''
        }
        return config
    },
    error => {
        Promise.reject(error)
    }
)

// respone拦截器
httpService.interceptors.response.use(
    response => {
        // 统一处理状态
        return response.data;
    }
)

/*网络请求部分*/

/*
 *  get请求
 *  url:请求地址
 *  params:参数
 * */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'GET',
            params: params
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  post请求
 *  url:请求地址
 *  params:参数
 * */
export function post(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'POST',
            data: qs.stringify(params),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(response => {
            console.log(response)
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

/*
 *  文件上传
 *  url:请求地址
 *  params:参数
 * */
export function fileUpload(url, params = {}) {
    return new Promise((resolve, reject) => {
        httpService({
            url: url,
            method: 'post',
            data: params,
            headers: { 'Content-Type': 'multipart/form-data' }
        }).then(response => {
            resolve(response);
        }).catch(error => {
            reject(error);
        });
    });
}

export default {
    get,
    post,
    fileUpload
}